package cn.openea.perpartner.common.core.utils;

import cn.hutool.core.util.StrUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则校验
 */
public class RegexUtil {
    private static final String EMAIL_RE = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    public static final String CELLPHONE = "^0?(13|15|16|18|14|17|19)[0-9]{9}$";  //
    public static final String CHINA_CELLPHONE = "^[0-9]*$"; //包括港澳台
    public static final String WIDE_CELLPHONE = "^[0-9－\\- +]+$"; //^[0-9－-]+\d*$
    public static final String HK_MACAO_TAIWAN = "^([6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]([8|6])\\d{5}$";//港澳台
    private static final String BARCODE = "^[1-9]\\d*$";
    private static final String NUMBER = "^[0-9]\\d+$";
    public static final String EMAIL_REGEX = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    public static final String GEN_REGEX = "^[a-zA-Z0-9\\-]+$";
    private static final String URL = "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
    private static final String PRICE_REGEX = "^\\d{1,10}$|^\\d{1,10}\\.\\d{0,1}\\w?$";
    private static final String WHOLE_NUMBER = "^\\+?[1-9][0-9]*$"; //正整数
    private static final String IP_REXP = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
    private static final String CHINESE_REGEX = "^[\\u4e00-\\u9fa5]+$";
    //private static final String HOME_PHONE = "^((0\\d{2,3}-?\\d{7,8})|(1[3584]\\d{9}))$";
    private static final String HOME_PHONE = "[0-9-()（）]{7,18}$";
    private static final String REAL_NUMBER = "^[-+]?\\d+(\\.\\d+)?$";

    private static final String AREA_CODE = "[+][0-9]\\d{0,2}$";
    private static final String TAX_NUM = "^[a-zA-Z0-9]+$";

    private static final String EMAIL_SALESUSER = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";//销售邮箱验证

//    private static final String HOME_CELL_PHONE ="^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$|^0\\d{2,3}-?\\d{7,8}$";    //同时校验手机号和固定电话

    private static final String HOME_CELL_PHONE = "^(13|15|16|18|14|17|19)[0-9]{9}$|^0\\d{2,3}-?\\d{7,8}$";  //支持所有手机号

    public static final String ORIGINAL_PHONE_123 = "^(13|15|18|14|17)[0-9]{9}$";

    public static final String ORIGINAL_PHONE_ONLINE = "^(13|15|18|14|17|16|19)[0-9]{9}$";

    // 密码 8位以上 可以包含特殊字符 必须有一个字母
    public static final String PASSWORD_MATCHER = "^(?=.*\\d)(?=.*[a-zA-Z]).{8,}$";

    public static final String REQUEST_URL = "(^(https|http)?:(?:\\/\\/))([a-z0-9]+([a-z0-9-]*(?:[a-z0-9]+))?\\.)?[a-z0-9]+([a-z0-9-]*(?:[a-z0-9]+))?(\\.us|\\.tv|\\.org\\.cn|\\.org|\\.net\\.cn|\\.net|\\.mobi|\\.me|\\.la|\\.info|\\.hk|\\.gov\\.cn|\\.edu|\\.com\\.cn|\\.com|\\.co\\.jp|\\.co|\\.cn|\\.cc|\\.biz)$";

    public static final String DOMAIN = "([a-z0-9]+([a-z0-9-]*(?:[a-z0-9]+))?\\.)?[a-z0-9]+([a-z0-9-]*(?:[a-z0-9]+))?(\\.us|\\.tv|\\.org\\.cn|\\.org|\\.net\\.cn|\\.net|\\.mobi|\\.me|\\.la|\\.info|\\.hk|\\.gov\\.cn|\\.edu|\\.com\\.cn|\\.com|\\.co\\.jp|\\.co|\\.cn|\\.cc|\\.biz)$";

    public static final String COLOR = "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$";

    public static final String NUMBER_OR_LETTER = "^[A-Za-z0-9]+$"; // 数字或字母

    public static final String URL_PARAM_REGEX = "(\\?|&+)(.+?)=([^&]*)";

    public static boolean match(String regex, String text) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchColor(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(COLOR);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchSalesUserEmail(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(EMAIL_SALESUSER);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


    public static boolean matchEmail(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(EMAIL_RE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchRegex(String text, String regex) {
        if (StrUtil.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchTaxNum(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(TAX_NUM);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


    public static boolean isContainsChinese(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Pattern pat = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher matcher = pat.matcher(str);
        return matcher.find();
    }

    public static boolean matchChinese(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(CHINESE_REGEX);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchChinaCellphone(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(CHINA_CELLPHONE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchCellphone(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(CELLPHONE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchWideCellphone(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(WIDE_CELLPHONE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchHomePhone(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(HOME_PHONE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchURL(String text) {
        Pattern pattern = Pattern.compile(URL);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchBarcode(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(BARCODE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


    public static boolean matchNumber(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(NUMBER);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean realNumber(String number) {
        if (StringUtils.isEmpty(number)) {
            return false;
        }
        Pattern pattern = Pattern.compile(REAL_NUMBER);
        Matcher matcher = pattern.matcher(number);
        return matcher.find();
    }

    public static boolean matchPrice(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(PRICE_REGEX);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean matchWholeNumber(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(WHOLE_NUMBER);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


    public static boolean matchIP(String ip) {
        if (ip.length() < 7 || ip.length() > 15 || "".equals(ip)) {
            return false;
        }
        Pattern pat = Pattern.compile(IP_REXP);

        Matcher mat = pat.matcher(ip);

        return mat.find();
    }

    public static String replacePhoneRegex(String phone) {
        if (StringUtils.isNotEmpty(phone)) {
            return StringUtils.unparseRequestHtml(phone).trim().replace(" ", "").replace("-", "").replace("(+86)", "").replace("+86", "");
        }
        return "";
    }

    public static boolean checkSmsIsChinaPhone(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        phone = replacePhoneRegex(phone);
        return RegexUtil.isChinaPhoneLegal(phone);
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 14+以5,7打头的数
     * 15+除4的任意数
     * 17+除2、4、9的任意数
     * 18+任意数
     */
    public static boolean isChinaPhoneLegal(String str) {
        //String regExp = "^(10[0-9]|13[0-9]|14[1456789]|15[0-3,5-9]|16[6]|17[01235678]|18[0-9]|19[89])\\d{8}$";

        String regExp = "^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0135678])|(18[0-9])|(19[89]))\\d{8})$";
        //String regExp = "^((13[0-9])|(14[5,7])|(15[^4])|(17[0,1,3,5-8])|(18[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }


    public static short matchesPhoneNumber(String phone_number) {
        String cn_mobile = "^((13[4-9])|(147)|(15[0-2,7-9])|(17[1,8])|(18[2-4,7-8]))\\d{8}$";  //中国移动
        String cn_unicom = "^((13[0-2])|(145)|(15[5-6])|(17[5-6])|(18[5-6]))\\d{8}$";        //中国联通
        String cn_telecom = "^((133)|(153)|(17[0,3,7])|(18[0-1,9]))\\d{8}$";                 //中国电信
        if (phone_number.matches(cn_mobile)) {
            return 1;
        } else if (phone_number.matches(cn_unicom)) {
            return 2;
        } else if (phone_number.matches(cn_telecom)) {
            return 3;
        } else {
            return 0;
        }
    }

    public static Object[] checkSmsPhone(String phone) {
        Object[] obj = new Object[]{false, ""};
        if (StringUtils.isNotEmpty(phone)) {
            phone = replacePhoneRegex(phone);
            obj[0] = RegexUtil.matchCellphone(phone);
            obj[1] = phone;
        }
        return obj;
    }

    public static boolean checkSendSmsPhone(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        phone = replacePhoneRegex(phone);
        return RegexUtil.matchCellphone(phone);
    }

    public static boolean matchAreaCode(String areaCode) {
        if (StringUtils.isEmpty(areaCode)) {
            return false;
        }
        Pattern pattern = Pattern.compile(AREA_CODE);
        Matcher matcher = pattern.matcher(areaCode);
        return matcher.find();
    }

    /**
     * 校验是否是手机号或固定电话
     *
     * @param text
     * @return
     */
    public static boolean matchHomeCellphone(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(HOME_CELL_PHONE);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    /**
     * 检查是否是合法域名
     */
    public static boolean isRequestUrl(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(REQUEST_URL);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }

    public static boolean isDomain(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        Pattern pattern = Pattern.compile(DOMAIN);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


}
